namespace jstsday3 {
    //for...of 案例
    //案例1
    function anli1(): void {
        const numbers: Array<number> = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
        for (const num of numbers) {
            if (num % 2 === 0) {
                console.log(num)
            }
        }
    }
    anli1()


    //案例2
    function anli2(): void {
        const phrases: Array<string> = ['helloworld', 'javascript is fun', 'openai gpt', 'coding is great'];
        for (const phrase of phrases) {
            if (phrase.includes('is'))
                console.log(phrase)
        }
    }
    anli2()

    //案例3
    function anli3(): void {
        const mixedNumbers: Array<number> = [-3, 5, -1, 8, -2, 7];
        let sum: number = 0;
        for (const num of mixedNumbers) {
            if (num > 0)
                sum += num;
        }
        console.log(`Total sum of positive numbers: ${sum}`)
    }
    anli3()

    //for...in 案例

    interface Person {
        [key: string]: string | number;
    }
    // interface Person{
    //     name:string,
    //     age:number,
    //     city:string,
    //     occupation:string
    // }
    //案例1

    function anli4(): void {
        const person1: Person = {
            name: 'Alice',
            age: 25,
            city: 'Wonderland',
            occupation: 'Engineer'
        }
        for (const key in person1) {
            console.log(person1[key])
        }
    }
    anli4()


    //案例2

    function anli5(): void {
        const data1: Person = {
            a: 1,
            b: 2,
            c: 'c',
            d: 4,
            e: 'e'
        }
        let sum1: number = 0;
        for (const key in data1) {
            if (typeof data1[key] === 'number')
                sum1 = data1[key] as number + sum1;
        }
        console.log(`Total sum of numbers: ${sum1}`)
    }
    anli5()


    //案例3

    function anli6(): void {
        const inventory: Person = {
            apples: 5,
            oranges: 0,
            bananas: 8,
            grapes: 2,
            cherries: 0
        }
        for (const key in inventory) {
            if (inventory[key as keyof Person] === 0)
                console.log(`${key}:${inventory[key]}`)
        }
    }
    anli6()


    //forEach 案例
    //案例1

    function anli7(): void {
        const numbers1 = [-3, 5, -1, 8, -2, 7]
        let sum2: number = 0;
        let count: number = 0;

        numbers1.forEach((num) => {
            if (num > 0) {
                sum2 += num;
                count++;
            }
        }
        )
        const average = sum2 / count;
        console.log(`Average of positive numbers: ${average}`)

        const words: Array<string> = ['apple', 'banana', 'cherry', 'date', 'fig', 'grape'];
        const minLength: number = 5;
        words.forEach((word) => {
            if (word.length > minLength) {
                console.log(word)
            }
        })
    }

    anli7()

    //map 案例
    //案例1

    function anli8(): void {
        const numbers: Array<number> = [-3, 5, -1, 8, -2, 7]
        const positiveNumbers = numbers.map(num => {
            return num < 0 ? -num : num
        })
        console.log(positiveNumbers)
    }
    anli8()

    //案例2

    function anli9(): void {
        const people: Array<object> = [
            { name: 'Alice', age: 25 },
            { name: 'Bob', age: 30 },
            { name: 'Charlie', age: 35 }
        ]
        const uppercasedNames = people.map(person => {
            const typeperson = person as { name: string, age: number }
            return typeperson.name.length > 3 ? {
                ...typeperson, name: typeperson.name.toUpperCase()
            } : typeperson
        })
        console.log(uppercasedNames)
    }
    anli9()

    //案例3

    function anli10(): void {
        const phrases1: Array<string> = ['hello world', 'javascript is fun', 'openai gpt', 'coding is great']
        const updatedPhrases = phrases1.map(phrase => {
            return phrase.includes('hello') ? phrase.replace('hello', 'hi') : phrase
        })
        console.log(updatedPhrases)
    }
    anli10()



    //使用 map、filter 和 reduce 方法结合的案例
    //案例1

    function anli11(): void {
        const phrases2: Array<string> = ['hello world', 'javascript', 'openai', 'coding is fun']
        const totalLength = phrases2.filter(phrase => phrase.length > 5).map(phrase => phrase.length).reduce((sum: number, length: number) => sum + length, 0)
        console.log(`Total length of phrases with more than 5 characters: ${totalLength}`)
    }

    anli11()


    //案例2

    function anli12(): void {
        const numbers3: Array<number> = [-5, -3, -2, 4, 6, -1];
        const squareSum = numbers3.filter(num => num > 0).map(num => num * num).reduce((sum: number, squared: number) => sum + squared, 0)
        console.log(`Sum of squares of positive numbers: ${squareSum}`)
    }
    anli12()

    //案例3

    function anli13(): void {
        const users = [
            { name: 'Alice', age: 22 },
            { name: 'Bob', age: 17 },
            { name: 'Charlie', age: 30 },
            { name: 'David', age: 15 }
        ]
        const averageAge = users.filter(user => user.age > 18).map(user => user.age).reduce((sum: number, age: number) => sum + age, 0) / users.filter(user => user.age > 18).length
        console.log(`Average age of users older than 18: ${averageAge}`)
    }
    anli13()



    //使用 findIndex、splice 方法结合案例

    function anli14(): object {
        const cart = [
            { id: 1, name: 'Laptop', quantity: 1 },
            { id: 2, name: 'Mouse', quantity: 2 },
            { id: 3, name: 'Keyboard', quantity: 1 }
        ];
        const itemIdToRemove = 2;
        const indexToRemove = cart.findIndex(item => item.id === itemIdToRemove);
        if (indexToRemove !== -1) {
            cart.splice(indexToRemove, 1);
        }
        return cart
    }
    console.log(anli14())

    //案例2

    function anli15(): object {
        const emplyees = [
            { id: 1, name: 'John', role: 'Developer' },
            { id: 2, name: 'Alice', role: 'Manager' },
            { id: 3, name: 'Bob', role: 'Engineer' }
        ]
        const emplyeeIdToUpdate = 3;
        const newRole = 'Senior Engineer'
        const indexToUpdate = emplyees.findIndex(emplyee => emplyee.id === emplyeeIdToUpdate)
        if (indexToUpdate !== -1) {
            emplyees[indexToUpdate].role = newRole
        }
        return emplyees
    }
    console.log(anli15())

    // 案例3

    function anli16(): void {
        const numbers4 = [4, -1, 6, -2, 7];
        const indexToReplace = numbers4.findIndex(num => num < 0);
        if (indexToReplace !== -1) {
            numbers4.splice(indexToReplace, 1, 0)
        }
        console.log(numbers4)
    }
    anli16()


}

